package com.techwells.applicationMarket.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import com.techwells.applicationMarket.dao.MessageMapper;
import com.techwells.applicationMarket.dao.UserMapper;
import com.techwells.applicationMarket.dao.UserMessageMapper;
import com.techwells.applicationMarket.domain.Message;
import com.techwells.applicationMarket.domain.User;
import com.techwells.applicationMarket.domain.UserMessage;
import com.techwells.applicationMarket.domain.rs.MessageVos;
import com.techwells.applicationMarket.properties.ConfigProperties;
import com.techwells.applicationMarket.service.MessageService;
import com.techwells.applicationMarket.util.Jpush;
import com.techwells.applicationMarket.util.PagingTool;
import com.techwells.applicationMarket.util.ResultInfo;

@Service
public class MessageServiceImpl implements MessageService{

	@Resource
	private MessageMapper messageMapper;
	
	@Resource
	private UserMapper userMapper;  
	
	@Resource
	private UserMessageMapper userMessageMapper;
	
	@Resource
	private ConfigProperties configProperties;
	
	@Override
	public Object addMessage(Message message)
			throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=messageMapper.insertSelective(message);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加失败");
			return resultInfo;
		}
		
		/*
		 * 极光推送给所有的用户，查询所有的用户Id
		 */
		List<User> users = userMapper.selectAllUserListBack();
		String[] alias=new String[users.size()];
		for (int i = 0; i < users.size(); i++) {
			alias[i]=users.get(i).getUserId()+"";  //添加用户Id到数组中
		}
		Jpush.push1(alias, message.getContent(), configProperties.getMessageType().getSystemMessageType());  //推送消息
		
		
		resultInfo.setMessage("添加成功");
		return resultInfo;
	}

	
	@Override
	public Object getMessage(Integer messageId,Integer userId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		
		
		Message message=messageMapper.selectByPrimaryKey(messageId);
		
		if (message==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该消息不存在");
			return resultInfo;
		}
		
		
		if (userId!=null) {
			/**
			 * 	修改该条消息的状态为已读
			 */
			UserMessage userMessage = userMessageMapper.selectByPrimaryKey(userId);
			
			if (userMessage==null) {
				userMessage=new UserMessage();
			}
			
			userMessage.setReadMessage(messageId+",");
			userMessage.setDelMessage("");
			
			int count = userMessageMapper.updateByPrimaryKeySelective(userMessage);
			if (count==0) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("获取消息失败");
				return resultInfo;
			}
		}
		
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(message);
		return resultInfo;
	}


	@Override
	public Object getMessageList(PagingTool pagingTool) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		int total=messageMapper.countTotal(pagingTool);  //总数量
		
		//如果总数为0，那么不需要再查询数据库了，直接返回即可
		if (total==0) {
			resultInfo.setMessage("获取成功");
			resultInfo.setTotal(total);
			resultInfo.setResult(new ArrayList<Message>());
			return resultInfo;
		}
		
		List<Message> messages=messageMapper.selectMessageList(pagingTool);   //获取列表
		
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(messages);
		resultInfo.setTotal(total);
		return resultInfo;
	}


	@Override
	public Object delete(Integer messageId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=messageMapper.deleteByPrimaryKey(messageId);
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("应用Id");
			return resultInfo;
		}
		
		resultInfo.setMessage("删除成功");
		return resultInfo;
	}


	@Override
	public Object modify(Message message) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		int count=messageMapper.updateByPrimaryKeySelective(message);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("修改失败");
			return resultInfo;
		}
		
		resultInfo.setMessage("修改成功");
		return resultInfo;
	}


	@Override
	public Object deleteMessageByUserId(Integer userId, Integer messageId) {
		ResultInfo resultInfo = new ResultInfo();
		Message message = messageMapper.selectByPrimaryKey(messageId);
		if (message==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该消息不存在");
			return resultInfo;
		}
		
		//删除这条消息
		UserMessage userMessage = userMessageMapper.selectByPrimaryKey(userId);
		
		if (userMessage==null) {
			userMessage=new UserMessage();
			userMessage.setDelMessage(messageId+",");
			userMessage.setReadMessage(messageId+"");  //删除消息也要将其设置为已读
		}else {
			userMessage.setDelMessage(userMessage.getDelMessage()+messageId+",");
			userMessage.setReadMessage(userMessage.getReadMessage()+messageId+""); //删除消息也要将其设置为已读
		}
		
		
		
		//更新
		int count = userMessageMapper.updateByPrimaryKeySelective(userMessage);
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("删除失败");
			return resultInfo;
		}
		
		resultInfo.setMessage("删除成功");
		return resultInfo;
	}


	@SuppressWarnings("unlikely-arg-type")
	@Override
	public Object getMessageListByUserId(PagingTool pagingTool) {
		ResultInfo resultInfo = new ResultInfo();
		
		int total=messageMapper.countTotalMessageVos(pagingTool);
		
		if (total==0) {
			resultInfo.setMessage("获取成功");
			resultInfo.setResult(null);
			resultInfo.setTotal(total);
			return resultInfo;
		}
		
		List<MessageVos> messageVos=messageMapper.selectMessageVosList(pagingTool);
		
		/**
		 * 	判断已读的状态
		 */
		//获取指定的用户Id的消息
		UserMessage userMessage = userMessageMapper.selectByPrimaryKey((int)pagingTool.getParams().get("userId"));
		if (userMessage!=null) {
			String readMessage = userMessage.getReadMessage();
			if (readMessage!=null) {  //如果已读信息不为空
				List<String> reads = Arrays.asList(readMessage.split(",")); 
				for (MessageVos message : messageVos) {
					if (reads.contains(message.getMessageId()+"")) {
						message.setReadStatus(1);  //设置已读
					}
				}
			}
			
		}
		
		
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(messageVos);
		resultInfo.setTotal(total);
		return resultInfo;
	}


	@Override
	public Object getMessageTotal(Integer userId) {
		ResultInfo resultInfo = new ResultInfo();
		int total=messageMapper.countTotalMessage(userId);
		resultInfo.setMessage("获取成功");
		resultInfo.setTotal(total);
		resultInfo.setResult(total);
		return resultInfo;
	}
	
	
	
	
	
	
}
